from django.db import models
from libs.timebase import Base


# 类别表
class CourseType(models.Model):
    title = models.CharField('类别表', max_length=16)
    sequence = models.IntegerField('展示顺序', null=True)

    class Meta:
        db_table = 'tb_coursetype'

    def __str__(self):
        return self.title


# 课程标签表
class CourseTag(models.Model):
    title = models.CharField('标签名字', max_length=32)
    sequence = models.IntegerField('展示顺序', null=True)

    class Meta:
        db_table = 'tb_coursetag'

    def __str__(self):
        return self.title


# 课程表
class Course(Base):
    OAUTHTYPE = (
        ('1', 'online'),
        ('2', 'soon'),
        ('3', 'offline'),
    )
    title = models.CharField('课程标题', max_length=64)
    desc = models.CharField('课程描述', max_length=200, null=True)
    img = models.ImageField('logo', upload_to='image', null=True, blank=True)
    course_type = models.ForeignKey(CourseType, on_delete=models.SET_NULL, null=True)
    course_tag = models.ForeignKey(CourseTag, on_delete=models.SET_NULL, null=True, )
    status = models.CharField('课程状态', choices=OAUTHTYPE, max_length=24)
    follows = models.IntegerField('关注人数', default=0)
    learner = models.IntegerField('学习人数', default=0)
    path = models.ManyToManyField('routes.PathModel')
    phases = models.ManyToManyField('routes.Phases', related_name='phase')

    class Meta:
        db_table = 'tb_course'

    def __str__(self):
        return str(self.title)
    # def __str__(self):
    #     return self.phases


# 课程章节表
class Chapter(models.Model):
    title = models.CharField('章节名称', max_length=16)
    chapter_num = models.IntegerField('章节号')
    course = models.ForeignKey(Course, on_delete=models.CASCADE, null=True)

    class Meta:
        db_table = 'tb_chapter'

    def __str__(self):
        return self.title


# 课程小节表
class CourseNode(models.Model):
    title = models.CharField('小节名称', max_length=16)
    node = models.IntegerField('小节号')
    chapter = models.ForeignKey(Chapter, on_delete=models.CASCADE, null=True, related_name='nodes')
    video = models.FileField("上传视频", upload_to="videos/%Y%m%d/", blank=True, max_length=1024)
    learn_time = models.IntegerField('学习小时', default=1)

    class Meta:
        db_table = 'tb_coursenode'


# 关注表
class Follow(Base):
    user = models.ForeignKey('user.User', on_delete=models.CASCADE, null=True)
    curriculum = models.ForeignKey(Course, on_delete=models.CASCADE, null=True)

    class Meta:
        db_table = 'tb_follow'


# 我的课程表
class MyCourse(Base):
    user = models.ForeignKey('user.User', on_delete=models.CASCADE, null=True
                             )
    course = models.ForeignKey('Course', on_delete=models.CASCADE, related_name='payuser')

    class Meta:
        db_table = 'tb_mycourse'

    def __str__(self):
        return "用户:%s, 课程:%s" % (self.user.username, self.course.title)


# 用户部分，关联课程的名字，课程的章小节
class UserSections(Base):
    user = models.ForeignKey('user.User', on_delete=models.PROTECT, related_name='usersections')
    course = models.ForeignKey(Course, on_delete=models.PROTECT, related_name='usersections')
    section = models.ForeignKey(CourseNode, on_delete=models.PROTECT, related_name='usersections')

    class Meta:
        db_table = 'tb_usersections'

    def __str__(self):
        return "用户:%s, 课程:%s, 小节: %s" % (self.user.username, self.course.title, self.section.title)

    # class Record(Base):
    #     title = models.CharField('节标题', max_length=24)
    #     serial_num = models.IntegerField('节序号')
    #     chapters = models.ForeignKey(Chapter, related_name='sections', on_delete=models.SET_NULL, null=True)
    #     learn_time = models.IntegerField('学习小时', default=1)
    #     video = models.FileField("上传视频", upload_to="videos/%Y%m%d/", max_length=1024,null=True,blank=True)
    #     seq_num = models.IntegerField("序号", default=1)
    #     tid = models.CharField(max_length=8,null=True)
    #
    #     class Meta:
    #         db_table = 'tb_record'

    # def __str__(self):
    #     return self.title


class Cuiwen(Base):
    username = models.CharField(max_length=20)
    c = models.ForeignKey(Course, on_delete=models.CASCADE, null=True)

    class Meta:
        db_table = 'tb_ccc'


# 评论
class Comment(Base):
    course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name='course_comment'
                               )
    user = models.ForeignKey('user.User', on_delete=models.SET_NULL, related_name='user_comment', null=True)
    content = models.CharField(max_length=500)

    class Meta:
        db_table = 'tb_comment'
        
#评论的回复
class CommentReply(Base):
    comment = models.ForeignKey(Comment, on_delete=models.CASCADE,related_name='repyl')
    user = models.ForeignKey('user.User',on_delete=models.SET_NULL, null=True,related_name='comment')
    to_user = models.ForeignKey('user.User', on_delete=models.SET_NULL, null=True, related_name='comment_to')
    content = models.CharField(max_length=500)

    class Meta:
        db_table = "tb_commentreply"

